package cn.tsvtc.shop.controller;
import cn.tsvtc.shop.constant.RedisConstant;
import cn.tsvtc.shop.entity.ResultMessage;
import cn.tsvtc.shop.feign.SmsFeign;
import cn.tsvtc.shop.pojo.RegisterEmailUser;
import cn.tsvtc.shop.pojo.RegisterPhoneUser;
import cn.tsvtc.shop.pojo.User;
import cn.tsvtc.shop.service.CodeService;
import cn.tsvtc.shop.service.UserService;
import cn.tsvtc.shop.utils.CodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;

import java.util.List;


/**
 * @author: Athena
 * @date: 2020/10/29 17:21
 * @description: 注册相关控制器
 */
@Controller
@RequestMapping("reg")
@Slf4j
public class RegisterController {

    private UserService userService;

    private CodeService codeService;

    private RedisTemplate redisTemplate;

    private SmsFeign smsFeign;

    @Autowired
    public RegisterController(UserService userService, CodeService codeService, RedisTemplate redisTemplate, SmsFeign smsFeign){
        this.userService=userService;
        this.codeService=codeService;
        this.redisTemplate=redisTemplate;
        this.smsFeign=smsFeign;
    }




    /**
     * 获取手机验证码，返回json
     * @param phone
     * @return
     */
    @GetMapping("code.action")
    @ResponseBody
    public ResultMessage<String> getCode(String phone){
        //检查是否redis已经存在相同用户名的用户
        User user=new User();
        user.setUsername(phone);
        Boolean duplicate = userService.checkUserDuplicate(user);
        if (duplicate) {
            //重复
            ResultMessage<String> failMessage = new ResultMessage<>(false,"failed","您已经注册过了");
            return failMessage;
        }else{
            //没有重复用户
            Boolean result = codeService.sendSmsCode(phone);
            if(result){
                ResultMessage<String> stringResultMessage = new ResultMessage<>();
                stringResultMessage.setData("成功");
                return stringResultMessage;
            }else {
                ResultMessage<String> failMessage = new ResultMessage<>(false,"failed","验证码获取失败");
                return failMessage;
            }
        }

    }

    /**
     * 使用邮箱注册
     * @param user
     * @return
     */
    @PostMapping("emailregister.action")
    @ResponseBody
    public ResultMessage<String> emailRegisterAction(@RequestBody RegisterEmailUser user){
        log.error("RegisterEmailUser===>{ }"+user);
        ResultMessage<String> insert = userService.insert(user);
        return insert;
    }

    /**
     * 使用手机号注册
     * @param user
     * @return
     */
    @PostMapping("smsregister.action")
    @ResponseBody
    public ResultMessage<String> smsRegisterAction(@RequestBody RegisterPhoneUser user){
        ResultMessage<String> insert = userService.insert(user);
        return insert;
    }


    /**
     * 邮箱激活
     * @param e
     * @param c
     * @return
     */
    @GetMapping("active.action")
    @ResponseBody
    public String active(String e,String c){
        Boolean result = codeService.activeUser(e, c);
        if(result){
            return "激活成功";
        }else {
            return "激活失败";
        }
    }
}
